package com.banzhi.shop.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.utils.ASUtils;
import com.squareup.picasso.Picasso;
import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.entity.shoppingcart.CartGoods;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;

import java.util.List;

/**
 * Created by LeiHe on 2017/4/26.
 */

public class CartAdapter extends BaseAdapter<CartGoods> {
    private CalculateMoneyCallBack callBack;
    public CartAdapter(Context context, List<CartGoods> data) {
        super(context, data);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final CartGoods data = getData().get(position);
        viewHolder holder = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = getInflater().inflate(R.layout.item_cart, null);
            holder.ivcheck = (ImageView) convertView.findViewById(R.id.iv_check);
            holder.ivimage = (ImageView) convertView.findViewById(R.id.iv_image);
            holder.tvtitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tvguige = (TextView) convertView.findViewById(R.id.tv_guige);
            holder.tvprice = (TextView) convertView.findViewById(R.id.tv_price);
            holder.tvjian = (TextView) convertView.findViewById(R.id.tv_jian);
            holder.tvjia = (TextView) convertView.findViewById(R.id.tv_jia);
            holder.tvnum = (TextView) convertView.findViewById(R.id.tv_num);
            holder.tvtextguige = (TextView) convertView.findViewById(R.id.tv_text_guige);
            holder.tvshancu = (TextView) convertView.findViewById(R.id.tvshanchu);
            convertView.setTag(holder);
        }
        holder = (viewHolder) convertView.getTag();
        if(ASUtils.isCanUse(data.getThumb())){
            Picasso.with(MyApp.context).load(data.getThumb()).placeholder(R.mipmap.zhanweitu2).into(holder.ivimage);
        }
        holder.tvtitle.setText(data.getTitle());
        if(ASUtils.isCanUse(data.getTitle1())){
            holder.tvguige.setText(data.getTitle1());
            holder.tvtextguige.setVisibility(View.VISIBLE);
        }else{
            holder.tvtextguige.setVisibility(View.GONE);
        }
        holder.tvnum.setText(data.getTotal()+"");
        if("1".equals(data.getSelected())){
            holder.ivcheck.setImageResource(R.mipmap.gouxuan);
        }else{
            holder.ivcheck.setImageResource(R.mipmap.tuoyuan);
        }
        if(data.getMarketprice()!=null){
            holder.tvprice.setText(NumberUtils.DecimalFormat(data.getMarketprice()));
        }
        //减的点击事件
        holder.tvjian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(data.getTotal()<=1){
                    ToastUtils.makeToastShort("受不了了，宝贝不能再减少了哦！");
                }else{
                    callBack.changeNum(data.getTotal()-1,position);
                }
            }
        });
        holder.tvjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.changeNum(data.getTotal()+1,position);
            }
        });
        holder.ivcheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("1".equals(data.getSelected())){
                    data.setSelected("0");
                }else{
                    data.setSelected("1");
                }
                callBack.changeState(data.getSelected(),position);
            }
        });
        holder.tvshancu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.deleteGoods(position);
            }
        });
        return convertView;
    }

    class viewHolder {
        ImageView ivcheck;
        ImageView ivimage;
        TextView tvtitle;
        TextView tvguige;
        TextView tvprice;
        TextView tvjian;
        TextView tvjia;
        TextView tvnum;
        TextView tvshancu;
        TextView tvtextguige;
    }
    public interface CalculateMoneyCallBack{
        void changeNum(int number,int position);
        void changeState(String selected,int position);
        void deleteGoods(int position);
    }
    public void CartCallBack(CalculateMoneyCallBack callBack){
        this.callBack=callBack;
    }


}
